﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="IdeController"
	type="http://metalx.org/Storage/Ide/IdeController"
	xmlns="http://metalx.org/Program"
	xmlns:cls="http://metalx.org/Class"
	xmlns:var="http://metalx.org/Variable"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:date="http://metalx.org/i386/Functions/Date"
	xmlns:time="http://metalx.org/i386/Functions/Time"
	xmlns:clk="http://metalx.org/i386/Functions/Clock"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:pcif="http://metalx.org/i386/Functions/Pci"
	xmlns:hex="http://metalx.org/i386/Functions/Hexadecimal"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:ari="http://metalx.org/Intel/80386/Arithmetic"
	xmlns:shift="http://metalx.org/Intel/80386/Shift"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:logic="http://metalx.org/Intel/80386/Logic"
	xmlns:usb="http://metalx.org/Usb/Ohci/Registers"
	xmlns:pci="http://metalx.org/Pc/Pci/Ports"
	xmlns:cfg="http://metalx.org/Pc/Pci/Configuration"
	xmlns:pata="http://metalx.org/Pc/Pata/Ports"
	xmlns:drive="http://metalx.org/Pc/Pata/DriveSelect"
	xmlns:sta="http://metalx.org/Pc/Pata/Status"
	xmlns:cmd="http://metalx.org/Pc/Pata/Commands"
	xmlns:id="http://metalx.org/Pc/Pata/DriveIdentity">

	<cls:field name="Device" type="http://metalx.org/Pci/PciDevice" offset="0"/>

	<cls:method name="GetHardDrives" type="http://metalx.org/Storage/Ide/IdeHardDriveList">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress0/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address1" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="0"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="12"/>

		<label id="address1"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress1/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address2" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="4"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="16"/>

		<label id="address2"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress2/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address3" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="24"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="36"/>

		<label id="address3"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress3/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="findHardDrives" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="28"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="40"/>

		<!--Find Hard Drives-->
		<label id="findHardDrives"/>
		
		<cpu:CopyImmediateToSI/>
		<addressOf ref="ideChannelTable" type="Absolute32"/>

		<cpu:CopyImmediateToBX/>
		<int>0</int>

		<cpu:CopyImmediateToCX/>
		<int>4</int>

		<label id="forEachChannel"/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>8</byte>

		<!--Select Drive-->
		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDXRegister/>
		<pata:DriveAndHead/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>4</byte>

		<cpu:OrALWithImmediate/>
		<binary>11100000</binary>

		<cpu:WriteALToDXPort/>

		<!--Send Identify Command-->
		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDXRegister/>
		<byte>1</byte>

		<cpu:CopyImmediateToAL/>
		<cmd:IdentifyDrive/>

		<cpu:WriteALToDXPort/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:CompareALToImmediate/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="nextChannel" type="Relative8"/>

		<label id="waitForBusy"/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:TestALWithImmediate/>
		<sta:Error/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="nextChannel" type="Relative8"/>

		<cpu:TestALWithImmediate/>
		<sta:DriveFault/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="nextChannel" type="Relative8"/>

		<cpu:TestALWithImmediate/>
		<sta:DataRequest/>

		<cpu:BranchToRelative8IfZero/>
		<addressOf ref="waitForBusy" type="Relative8"/>
		
		<!--Read Data-->
		<!--<cpu:PushDXToStack/>
		<cpu:PushCXToStack/>
		
		<cpu:MathImmediate8ToOperandFunction/>
		<math:SubtractFromDXRegister/>
		<byte>7</byte>

		<cpu:CopyImmediateToCX/>
		<int>128</int>

		<cpu:Repeat/>
		<cpu:ReadFromDXPortToAX/>

		<cpu:PullCXFromStack/>
		<cpu:PullDXFromStack/>-->

		<cpu:LogicFunction/>
		<logic:PushSIAddressPlusImmediate8/>
		<byte>8</byte>

		<cpu:LogicFunction/>
		<logic:PushSIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:LogicFunction/>
		<logic:PushSIAddress/>

		<cpu:IncrementBX/>

		<label id="nextChannel"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSIRegister/>
		<byte>12</byte>

		<cpu:LoopToRelative8/>
		<addressOf ref="forEachChannel" type="Relative8"/>

		<!--Create List-->
		<cpu:CopyRegisterToOperand/>
		<op:BX-AXRegister/>

		<lst:Create/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:CompareWithBXRegister/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfNotEqual/>
		<addressOf ref="findClass" type="Relative8"/>

		<cpu:JumpToRelative/>
		<addressOf ref="createObject" type="Relative32"/>
		
		<!--Find IdeHardDrive Class-->
		<label id="findClass"/>
		
		<cpu:PushDIToStack/>

		<sys:GetCatalog/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="ideHardDrive" type="Absolute32"/>

		<cat:FindType/>

		<cpu:CopyRegisterToOperand/>
		<op:BX-CXRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-BXRegister/>

		<cpu:PullDIFromStack/>

		<!--Create IdeHardDrive Objects-->
		<label id="forEachDevice"/>

		<cpu:PushDIToStack/>

		<mm:CheckOut length="9"/>

		<cpu:PullSIFromStack/>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddress/>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-DIAddressPlusImmediate8/>
		<byte>8</byte>

		<cpu:PushSIToStack/>

		<cpu:CopyImmediateToAX/>
		<int>9</int>

		<obj:Create/>

		<cpu:PullSIFromStack/>

		<cpu:PushSIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:CX-AXRegister/>

		<cpu:DecrementAX/>

		<lst:SetItem/>

		<cpu:PullDIFromStack/>

		<cpu:LoopToRelative8/>
		<addressOf ref="forEachDevice" type="Relative8"/>

		<!--Create List Object-->
		<label id="createObject"/>
		
		<!--Get Item Count-->
		<cpu:CopyOperandToRegister/>
		<op:AX-DIAddress/>

		<cpu:IncrementAX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>2</byte>

		<cpu:PushAXToStack/>
		<cpu:PushSIToStack/>
		<cpu:PushDIToStack/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="ideHardDriveList" type="Absolute32"/>

		<sys:GetCatalog/>

		<cat:FindType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-BXRegister/>

		<cpu:PullDIFromStack/>
		<cpu:PullSIFromStack/>
		<cpu:PullAXFromStack/>

		<obj:Create/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetFloppyDrives" type="http://metalx.org/Storage/FloppyDriveList">
		<sys:GetCatalog/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="className" type="Absolute32"/>

		<cat:FindType/>

		<cpu:PushSIToStack/>

		<cpu:CopyImmediateToAX/>
		<int>0</int>

		<lst:Create/>

		<cpu:CopyImmediateToAX/>
		<int>4</int>

		<cpu:PullBXFromStack/>

		<obj:Create/>

		<cpu:ReturnToNearCaller/>

		<var:string id="className">http://metalx.org/Storage/FloppyDriveList</var:string>
	</cls:method>
	
	<cls:method name="GetCdDrives" type="http://metalx.org/Storage/Ide/IdeCdDriveList">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress0/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address1" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="0"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="12"/>

		<label id="address1"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress1/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address2" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="4"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="16"/>

		<label id="address2"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress2/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="address3" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="24"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="36"/>

		<label id="address3"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<pcif:GetBaseAddress3/>

		<cpu:AndAXWithImmediate/>
		<hex>fffffffc</hex>

		<cpu:CompareAXToImmediate/>
		<int>0</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="findCdDrives" type="Relative8"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="28"/>

		<cpu:CopyAXToAbsoluteMemory/>
		<addressOf ref="ideChannelTable" type="Absolute32" offset="40"/>

		<!--Find Cd Drives-->
		<label id="findCdDrives"/>
		
		<cpu:CopyImmediateToSI/>
		<addressOf ref="ideChannelTable" type="Absolute32"/>

		<cpu:CopyImmediateToBX/>
		<int>0</int>

		<cpu:CopyImmediateToCX/>
		<int>4</int>

		<label id="forEachChannel"/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>8</byte>

		<!--Select Drive-->
		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDXRegister/>
		<pata:DriveAndHead/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>4</byte>

		<cpu:OrALWithImmediate/>
		<binary>11100000</binary>

		<cpu:WriteALToDXPort/>

		<!--Send Identify Command-->
		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDXRegister/>
		<byte>1</byte>

		<cpu:CopyImmediateToAL/>
		<cmd:IdentifyAtapi/>

		<cpu:WriteALToDXPort/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:CompareALToImmediate/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="nextChannel" type="Relative8"/>

		<label id="waitForBusy"/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:TestALWithImmediate/>
		<sta:Error/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="nextChannel" type="Relative8"/>

		<cpu:TestALWithImmediate/>
		<sta:Busy/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="waitForBusy" type="Relative8"/>

		<!--Read Data-->
		<!--<cpu:PushDXToStack/>
		<cpu:PushCXToStack/>
		
		<cpu:MathImmediate8ToOperandFunction/>
		<math:SubtractFromDXRegister/>
		<byte>7</byte>

		<cpu:CopyImmediateToCX/>
		<int>128</int>

		<cpu:Repeat/>
		<cpu:ReadFromDXPortToAX/>

		<cpu:PullCXFromStack/>
		<cpu:PullDXFromStack/>-->

		<cpu:LogicFunction/>
		<logic:PushSIAddressPlusImmediate8/>
		<byte>8</byte>

		<cpu:LogicFunction/>
		<logic:PushSIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:LogicFunction/>
		<logic:PushSIAddress/>

		<cpu:IncrementBX/>

		<label id="nextChannel"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSIRegister/>
		<byte>12</byte>

		<cpu:LoopToRelative8/>
		<addressOf ref="forEachChannel" type="Relative8"/>

		<!--Create List-->
		<cpu:CopyRegisterToOperand/>
		<op:BX-AXRegister/>

		<lst:Create/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:CompareWithBXRegister/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfNotEqual/>
		<addressOf ref="findClass" type="Relative8"/>

		<cpu:JumpToRelative/>
		<addressOf ref="createObject" type="Relative32"/>

		<!--Find IdeCdDrive Class-->
		<label id="findClass"/>
		
		<cpu:PushDIToStack/>

		<sys:GetCatalog/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="ideCdDrive" type="Absolute32"/>

		<cat:FindType/>

		<cpu:CopyRegisterToOperand/>
		<op:BX-CXRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-BXRegister/>

		<cpu:PullDIFromStack/>

		<!--Create IdeCdDrive Objects-->
		<label id="forEachDevice"/>

		<cpu:PushDIToStack/>

		<mm:CheckOut length="9"/>

		<cpu:PullSIFromStack/>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddress/>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-DIAddressPlusImmediate8/>
		<byte>8</byte>

		<cpu:PushSIToStack/>

		<cpu:CopyImmediateToAX/>
		<int>9</int>

		<obj:Create/>

		<cpu:PullSIFromStack/>

		<cpu:PushSIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:CX-AXRegister/>

		<cpu:DecrementAX/>

		<lst:SetItem/>

		<cpu:PullDIFromStack/>

		<cpu:LoopToRelative8/>
		<addressOf ref="forEachDevice" type="Relative8"/>

		<!--Create List Object-->
		<label id="createObject"/>

		<!--Get Item Count-->
		<cpu:CopyOperandToRegister/>
		<op:AX-DIAddress/>

		<cpu:IncrementAX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>2</byte>

		<cpu:PushAXToStack/>
		<cpu:PushSIToStack/>
		<cpu:PushDIToStack/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="ideHardDriveList" type="Absolute32"/>

		<sys:GetCatalog/>

		<cat:FindType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-BXRegister/>

		<cpu:PullDIFromStack/>
		<cpu:PullSIFromStack/>
		<cpu:PullAXFromStack/>

		<obj:Create/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyImmediateToDI/>
		<addressOf ref="deviceName" type="Absolute32"/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>

		<var:string id="deviceName">IDE Controller</var:string>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<var:string id="ideHardDrive">http://metalx.org/Storage/Ide/IdeHardDrive</var:string>
	<var:string id="ideHardDriveList">http://metalx.org/Storage/Ide/IdeHardDriveList</var:string>
	<var:string id="ideCdDrive">http://metalx.org/Storage/Ide/IdeCdDrive</var:string>
	<var:string id="ideCdDriveList">http://metalx.org/Storage/Ide/IdeCdDriveList</var:string>
	
	<label id="ideChannelTable"/>
	<hex>000001f0</hex>
	<hex>000003f6</hex>
	<int>0</int>

	<hex>000001f0</hex>
	<hex>000003f6</hex>
	<int>1</int>

	<hex>00000170</hex>
	<hex>00000376</hex>
	<int>0</int>

	<hex>00000170</hex>
	<hex>00000376</hex>
	<int>1</int>
</cls:class>